/**
 * For each iterable variable in an object, function func will be called on it.
 * @param obj      The object to iterate.
 * @param func     The function to call.
 */
function foreach( obj, func )
{
   for( var it in obj )
   {
      obj[ it ] = func.call( window, obj[ it ] );
   }
}

/**
 * This will probably be used in conjunction with the above.  If a value !isNaN, it
 * will be returned as a number.  Otherwise it will be returned as a whatever it was.
 * @param num     An object of uncertain numerical status.
 * @returns       A number if it is a number, or whatever it was.
 */
function castToNumber( num )
{
   if( !isNaN( num ) )
   {
      return Number( num );
   }
   else
   {
      return num;
   }
}

/**
 * @see castToNumber
 */
function castToBoolean( bool )
{
   if( !bool || bool.toString().toLowerCase() == "false" )
   {
      return false;
   }
   else 
   {
      return true;
   }
}

/**
 * A means of generating basic hash codes to represent different URL's for the window restoration.
 * For now we only need a unique-"ish" identifier because it is mostly to guarantee that the hash
 * will be consistent to a URL instead of exclusive.
 * @param toHash    The value to hash
 * @returns         A numeric representation
 */
// TODO: Figure out why this needs to be here and can't be in value_manipulation.js
function getHash( toHash )
{
   toHash = toHash.toString();
   var id = 0;
   for(var i=0; i<toHash.length; i++) 
   {
      id += (toHash.charCodeAt(i))*(i+1);
   }
   return id;
}


/**
 * A VERY basic serialization function which returns the variables part of the GET request.
 * @param params   Object which represets the key/value pairs to be serialized
 * @param comb     (Optional) the value joining different variables
 * @return         The combined string.
 */
function parseParams( params, comb )
{
   if( !comb ) comb = "&";
   var ret = "";
   for( var it in params )
   {
      if( ret.length ) ret += comb;
      ret += it + "=" + params[ it ];
   }
   return ret;
}

function compress( data )
{
    var q = {}; var ret = ";";
    data.replace(/([^=&amp;]+)=([^&amp;]*)/g, function(m, key, value){
        q[key] = (q[key] ? q[key] + ";," : ";") + value;
    });
    for ( var key in q )
        ret = (ret ? ret + ";&amp;" : ";") + key + ";=" + q[key];
    return ret;
}
